﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;
using OutillageTests;

namespace GestionnaireTUPleiade
{
    public class Core
    {
        private static Core instance = null;
        
        private static Dictionary<Type, Dictionary<MethodInfo, bool>> businessMap = null;
        public static string businessAssemblyPath = @"C:\TFS\Fwk\Main\Metier\Bin\";
        public static string testsAssemblyPath = @"C:\TFS\Fwk\Main\Metier\Bin\Tests\";
        public static string xmlPath = @"C:\TFS\Fwk\Main\Metier\Bin\Tests\ParametrageTests\";
        public static string utClassesPath = @"C:\TFS\Fwk\Main\Metier\Bin\Tests\ClassesTests\";
                
        private Core()
        {
            businessMap = new Dictionary<Type, Dictionary<MethodInfo, bool>>();
        }
        
        public static Core GetInstance()
        {
            if (instance == null)
                instance = new Core();
            return instance;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns>Retourne la map actuellement chargée</returns>
        public Dictionary<Type, Dictionary<MethodInfo, bool>> GetBusinessMap()
        {
            return businessMap;
        }

        /// <summary>
        /// Charge une map par reflexion sur un ensemble de DLL
        /// </summary>
        /// <param name="path">Le chemin d'accès aux DLL</param>
        /// <param name="searchPattern">Le pattern de recherche spécifique</param>
        public void LoadBusinessMap(string searchPattern)
        {
            
            
            string[] files = Directory.GetFiles(businessAssemblyPath, searchPattern);

            //try
            //{
                foreach (string file in files)
                {
                    Assembly assembly = Assembly.LoadFile(file);
                    foreach (Type classe in assembly.GetExportedTypes())
                    {
                        if (classe.GetInterfaces().Count() > 0 &&
                            classe.GetInterfaces().Where(x => x.FullName.Contains("Adp.Pleiade.Metier")).Count() > 0)
                        {
                            ClassParser parser = new ClassParser(classe);
                            businessMap.Add(classe, parser.ParseClass());
                        }
                    }
                }
            //}
            //catch
            //{
            //}
        }

        /// <summary>
        /// Génère l'ensemble des fichiers XML de configuration de tests sur la map
        /// </summary>
        public void GenerateXML()
        {
            businessMap = GenerationXML.GenererXML(xmlPath, businessMap);
        }

        public void GenerateUTClasses()
        {
            GenerationClasseTU.GenererClassesTU(utClassesPath, businessMap);            
        }

        public void SetBusinessAssemblyPath(string aPath)
        {
            businessAssemblyPath = aPath;
        }

        public void SetTestsAssemblyPath(string aPath)
        {
            testsAssemblyPath = aPath;
        }

        public string GetBusinessAssemblyPath()
        {
            return businessAssemblyPath;
        }

        public string GetTestsAssemblyPath()
        {
            return testsAssemblyPath;
        }

        public void SetxmlPath(string aPath)
        {
            xmlPath = aPath;
        }

        public void SetUTClassesPath(string aPath)
        {
            utClassesPath = aPath;
        }

        public string GetxmlPath()
        {
            return xmlPath;
        }

        public string GetUTClassesPath()
        {
            return utClassesPath;
        }
    }
}
